home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gp_wgetv.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  3.8 KB  |  137 lines

  1. /* Copyright (C) 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gp_wgetv.c,v 1.3 2000/09/19 19:00:25 lpd Exp $ */
  20. /* MS Windows implementation of gp_getenv */
  21.  
  22. #include <windows.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>        /* for getenv */
  25. #include <string.h>
  26. #include "gscdefs.h"        /* for gs_productfamily and gs_revision */
  27.  
  28. /* prototypes */
  29. int gp_getenv_registry(HKEY hkeyroot, const char *key, const char *name, 
  30.     char *ptr, int *plen);
  31.  
  32. /* ------ Environment variables ------ */
  33.  
  34. /* Get the value of an environment variable.  See gp.h for details. */
  35. int 
  36. gp_getenv(const char *name, char *ptr, int *plen)
  37. {
  38.     const char *str = getenv(name);
  39.  
  40.     if (str) {
  41.     int len = strlen(str);
  42.  
  43.     if (len < *plen) {
  44.         /* string fits */
  45.         strcpy(ptr, str);
  46.         *plen = len + 1;
  47.         return 0;
  48.     }
  49.     /* string doesn't fit */
  50.     *plen = len + 1;
  51.     return -1;
  52.     }
  53.     /* environment variable was not found */
  54.  
  55. #ifdef __WIN32__
  56.     {
  57.     /* If using Win32, look in the registry for a value with
  58.      * the given name.  The registry value will be under the key
  59.      * HKEY_CURRENT_USER\Software\AFPL Ghostscript\N.NN
  60.      * or if that fails under the key
  61.      * HKEY_LOCAL_MACHINE\Software\AFPL Ghostscript\N.NN
  62.      * where "AFPL Ghostscript" is actually gs_productfamily
  63.      * and N.NN is obtained from gs_revision.
  64.      */
  65.     DWORD version = GetVersion();
  66.  
  67.     if (!(((HIWORD(version) & 0x8000) != 0)
  68.           && ((HIWORD(version) & 0x4000) == 0))) {
  69.         /* not Win32s */
  70.         int code;
  71.         char key[256];
  72.         char dotversion[16];
  73.         
  74.         if (gs_revision % 100 == 0)
  75.         sprintf(dotversion, "%d.0", (int)(gs_revision / 100));
  76.         else
  77.         sprintf(dotversion, "%d.%02d", (int)(gs_revision / 100),
  78.             (int)(gs_revision % 100));
  79.         sprintf(key, "Software\\%s\\%s", gs_productfamily, dotversion);
  80.  
  81.         code = gp_getenv_registry(HKEY_CURRENT_USER, key, name, ptr, plen);
  82.         if ( code <= 0 )
  83.         return code;    /* found it */
  84.  
  85.         code = gp_getenv_registry(HKEY_LOCAL_MACHINE, key, name, ptr, plen);
  86.         if ( code <= 0 )
  87.         return code;    /* found it */
  88.     }
  89.     }
  90. #endif
  91.  
  92.     /* nothing found at all */
  93.  
  94.     if (*plen > 0)
  95.     *ptr = 0;
  96.     *plen = 1;
  97.     return 1;
  98. }
  99.  
  100.  
  101. /*
  102.  * Get a named registry value.
  103.  * Key = hkeyroot\\key, named value = name.
  104.  * name, ptr, plen and return values are the same as in gp_getenv();
  105.  */
  106.  
  107. int 
  108. gp_getenv_registry(HKEY hkeyroot, const char *key, const char *name, 
  109.     char *ptr, int *plen)
  110. {
  111.     HKEY hkey;
  112.     DWORD cbData, keytype;
  113.     BYTE b;
  114.     LONG rc;
  115.     BYTE *bptr = (BYTE *)ptr;
  116.  
  117.     if (RegOpenKeyEx(hkeyroot, key, 0, KEY_READ, &hkey)
  118.     == ERROR_SUCCESS) {
  119.     keytype = REG_SZ;
  120.     cbData = *plen;
  121.     if (bptr == (char *)NULL)
  122.         bptr = &b;    /* Registry API won't return ERROR_MORE_DATA */
  123.             /* if ptr is NULL */
  124.     rc = RegQueryValueEx(hkey, (char *)name, 0, &keytype, bptr, &cbData);
  125.     RegCloseKey(hkey);
  126.     if (rc == ERROR_SUCCESS) {
  127.         *plen = cbData;
  128.         return 0;    /* found environment variable and copied it */
  129.     } else if (rc == ERROR_MORE_DATA) {
  130.         /* buffer wasn't large enough */
  131.         *plen = cbData;
  132.         return -1;
  133.     }
  134.     }
  135.     return 1;    /* not found */
  136. }
  137.